package com.applink.sell_proof.builder;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.applink.sell_proof.R;
import com.applink.sell_proof.SearchProductModel;
import com.applink.sell_proof.model.Product;

public class ProofBuilder extends AbstractListBuilder implements
		OnClickListener {

	protected int mSelectedPosition = -1;

	public ProofBuilder(Context context, ViewGroup parent) {
		super(context, parent);
	}

	@Override
	public void notifyDataChange() {
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void buildData() {
	}

	@SuppressWarnings("unchecked")
	public void addProduct() {
		((ArrayList<Product>) mData).add(0, new Product());
		notifyDataChange();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.model_no:
			mSelectedPosition = (Integer) v.getTag();
			Intent intent = new Intent(mContext, SearchProductModel.class);
			((Activity) mContext).startActivityForResult(intent,
					SearchProductModel.SEARCH_PRODUCT);
			break;
		case R.id.pdremove:
			int position = (Integer) v.getTag();
			mData.remove(position);
			notifyDataChange();
			break;
		default:
			break;
		}
	}

	public void setProductModel(Product productModel) {
		Product model = (Product) mData.get(mSelectedPosition);
		model.setModelName(productModel.getModelName());
		model.setModelNo(productModel.getModelNo());
		model.setProductId(productModel.getProductId());
		notifyDataChange();
	}

	// private class PriceTextWatcher implements TextWatcher {
	// private View editText;
	//
	// public PriceTextWatcher(View editText) {
	// this.editText = editText;
	// }
	//
	// @Override
	// public void afterTextChanged(Editable s) {
	//
	// if (s.length() > 0) {
	// int selectedPosition = (Integer) editText.getTag();
	// Product currentModel = (Product) mData.get(selectedPosition);
	// currentModel.setPrice(Float.parseFloat(s.toString()));
	// }
	// }
	//
	// @Override
	// public void beforeTextChanged(CharSequence s, int start, int count,
	// int after) {
	// }
	//
	// @Override
	// public void onTextChanged(CharSequence s, int start, int before,
	// int count) {
	// }
	//
	// }

	private class QuantityTextWatcher implements TextWatcher {
		private View editText;

		public QuantityTextWatcher(View editText) {
			this.editText = editText;
		}

		@Override
		public void afterTextChanged(Editable s) {

			if (s.length() > 0) {
				int selectedPosition = (Integer) editText.getTag();
				Product currentModel = (Product) mData.get(selectedPosition);
				currentModel.setQuantity(Integer.parseInt(s.toString()));
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}
	}
	
	private class ServiceTagTextWatcher implements TextWatcher {
		private View editTextService;

		public ServiceTagTextWatcher(View editText) {
			this.editTextService = editText;
		}

		@Override
		public void afterTextChanged(Editable s) {

			if (s.length() > 0) {
				int selectedPosition = (Integer) editTextService.getTag();
				Product currentModel = (Product) mData.get(selectedPosition);
				currentModel.setService_tag(s.toString());//(Integer.parseInt(s.toString()));
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}
	}

	@Override
	public View buildConvertView() {

		View layout = mLayoutInflater.inflate(R.layout.proof_item, null);
		layout.findViewById(R.id.model_no).setOnClickListener(this);

		((EditText) layout.findViewById(R.id.quantity))
				.addTextChangedListener(new QuantityTextWatcher(layout
						.findViewById(R.id.quantity)));
		((EditText) layout.findViewById(R.id.service_tag))
		.addTextChangedListener(new ServiceTagTextWatcher(layout
				.findViewById(R.id.service_tag)));

		final Button btn = new Button(mContext);
		btn.setId(R.id.pdremove);
		btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));

		btn.setText("Remove Product");

		btn.setClickable(true);
		btn.setOnClickListener(this);

		((LinearLayout) layout).addView(btn);

		return layout;
	}

	@Override
	public ConcurrentHashMap<Integer, View> buildConvertViewContent(View base) {

		ConcurrentHashMap<Integer, View> views = new ConcurrentHashMap<Integer, View>();

		views.put(R.id.model_no, base.findViewById(R.id.model_no));
		views.put(R.id.quantity, base.findViewById(R.id.quantity));
		views.put(R.id.service_tag, base.findViewById(R.id.service_tag));

		View delBtn = base.findViewById(R.id.pdremove);
		if (delBtn != null)
			views.put(R.id.pdremove, delBtn);

		return views;
	}

	@Override
	public void builConvertViewContentData(int position,
			ConcurrentHashMap<Integer, View> views, List<? extends Object> data) {

		Product model = (Product) mData.get(position);

		views.get(R.id.model_no).setTag(position);
		views.get(R.id.quantity).setTag(position);
		views.get(R.id.service_tag).setTag(position);

		((TextView) views.get(R.id.model_no)).setText(model.getModelNo());

		((TextView) views.get(R.id.quantity)).setText(Integer.toString(model
				.getQuantity()));
		((TextView) views.get(R.id.service_tag)).setText((model
				.getService_tag()));

		View delBtn = views.get(R.id.pdremove);
		if (delBtn == null)
			return;

		delBtn.setTag(position);

		if (mData.size() <= 1) {
			delBtn.setVisibility(View.GONE);
		} else {
			if (position == 0)
				delBtn.setVisibility(View.VISIBLE);
		}		
	}
}